package com.itheima.mm.service.Imp;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mm.dto.QuestionsDto;
import com.itheima.mm.dto.R;
import com.itheima.mm.mapper.*;
import com.itheima.mm.pojo.*;
import com.itheima.mm.service.QuestionsService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Service
public class QuestionsServiceImpl implements QuestionsService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private QuestionsMapper questionsMapper;
    @Autowired
    private QuestionsOptionsMapper questionsOptionsMapper;
    @Autowired
    private QuestionsRecordsMapper questionsRecordsMapper;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private AuditOpinionsMapper auditOpinionsMapper;
    @Autowired
    private RecoredsMapper recoredsMapper;

    //6.1审核意见
    @Override
    public R selectOpinion(Integer id) {
        LambdaQueryWrapper<AuditOpinions> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AuditOpinions::getId, id);
        //查询审核意见项
        AuditOpinions auditOpinions = auditOpinionsMapper.selectOne(wrapper);
        //根据审核人id查询审核人以便补全姓名
        User user = userMapper.selectById(auditOpinions.getCheckerId());
        Opinions opinions = new Opinions();
        opinions.setChecker(user.getUsername());
        //意见
        opinions.setOpinion(auditOpinions.getRemarks());
        //时间
        opinions.setChkTime(auditOpinions.getChkTime().toString());
        return new R(opinions);
    }

    //6.2出题记录
    @Override
    public R selectRecords(Integer id) {
        LambdaQueryWrapper<Records> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Records::getId, id);
        Records records = recoredsMapper.selectOne(wrapper);
        SelectRecords selectRecords = new SelectRecords();
        // 操作
        selectRecords.setOperation(records.getOperation());
        // 出题人
        User user = userMapper.selectById(id);
        selectRecords.setSetter(user.getUsername());
        // 出题时间
        selectRecords.setSetTime(records.getSetTime() + "");
        return new R(selectRecords);
    }

    //6.3基础列表查询
    @Override
    public PageResult findAll(Integer page, Integer pagesize, String subjectID, String difficulty, String questionType, String tags, String province, String city, String keyword, String remarks, String shortName, String direction, String creatorID, String catalogID) {
        IPage<Questions> p = new Page<>(page, pagesize);
        LambdaQueryWrapper<Questions> wrpper = new LambdaQueryWrapper<>();
        if (subjectID != null) wrpper.eq(Questions::getSubjectId, subjectID);
        if (difficulty != null) wrpper.eq(Questions::getDifficulty, difficulty);
        if (questionType != null) wrpper.eq(Questions::getQuestionnType, questionType);
        if (tags != null) wrpper.eq(Questions::getTags, tags);
        if (province != null) wrpper.eq(Questions::getProvince, province);
        if (city != null) wrpper.eq(Questions::getCity, city);
        //题干
        if (keyword != null) wrpper.like(Questions::getQuestion, keyword);
        //题目备注
        if (remarks != null) wrpper.like(Questions::getRemarks, remarks);
        if (creatorID != null) wrpper.eq(Questions::getCreatorId, creatorID);
        if (catalogID != null) wrpper.eq(Questions::getCatalogId, catalogID);
        wrpper.eq(Questions::getIsChoice, 0);
        IPage<Questions> questionsIPage = questionsMapper.selectPage(p, wrpper);

        return new PageResult(questionsIPage.getTotal(),questionsIPage.getPages(),questionsIPage.getPages(),questionsIPage.getCurrent(),questionsIPage.getRecords());
    }

    //6.4基础题库删除
    @Override
    public void deleteById(Integer id) {

        LambdaUpdateWrapper<Questions> wrpper = new LambdaUpdateWrapper<>();
        wrpper.eq(Questions::getId, id).eq(Questions::getIsChoice, 0);
        questionsMapper.delete(wrpper);

    }

    //6.5基础题库添加
    @Override
    public R addQestion(QuestionsDto questionsDto) {
        Questions questions = new Questions();
        BeanUtils.copyProperties(questionsDto, questions);
        questions.setIsChoice(0);
        questionsMapper.insert(questions);
        List<QuestionsOptions> options = questionsDto.getOptions();
        for (QuestionsOptions option : options) {
            option.setQuestionsId(questions.getId());
            questionsOptionsMapper.insert(option);
        }
        LambdaQueryWrapper<Questions> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Questions::getNumber, questionsDto.getNumber());
        Questions questions1 = questionsMapper.selectOne(wrapper);
        return new R(questions1.getId());
    }

    //6.5基础题库查询
    @Override
    public QuestionsDto selectById(Integer id) {
        LambdaUpdateWrapper<Questions> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Questions::getId, id).eq(Questions::getIsChoice, 0);
        Questions questions = questionsMapper.selectOne(wrapper);
        QuestionsDto questionsDto = new QuestionsDto();
        BeanUtils.copyProperties(questions, questionsDto);
        LambdaQueryWrapper<QuestionsOptions> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(QuestionsOptions::getQuestionsId, id);
        List<QuestionsOptions> questionsOptions = questionsOptionsMapper.selectList(wrapper1);
        questionsDto.setOptions(questionsOptions);
        return questionsDto;


    }

    //6.6 修改基础题库信息
    @Override
    public void modifyById(Integer id, QuestionsDto questionsDto) {
        Questions questions = new Questions();
        BeanUtils.copyProperties(questionsDto, questions);
        LambdaUpdateWrapper<Questions> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Questions::getId, id).eq(Questions::getIsChoice, 0);
        questionsMapper.update(questions, wrapper);
        //根据questionId 删除option数据
        LambdaUpdateWrapper<QuestionsOptions> wrapper1 = new LambdaUpdateWrapper<>();
        wrapper1.eq(QuestionsOptions::getQuestionsId, id);
        questionsOptionsMapper.delete(wrapper1);
        //将更改后的数据添加到option表中
        List<QuestionsOptions> options = questionsDto.getOptions();
        for (QuestionsOptions option : options) {
            option.setQuestionsId(id);
            questionsOptionsMapper.insert(option);
        }

    }

    //6.10精品题库上下架
    @Override
    public void mofifyStatus(Integer questionId, Integer operationId) {
        //将对应的id 的question对象的状态修改
        Questions questions = questionsMapper.selectById(questionId);
        questions.setChkState(operationId);
        LambdaUpdateWrapper<Questions> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Questions::getId, questionId);
        questionsMapper.update(questions, wrapper);
        //将修改动作存储起来
        QuestionsRecords questionsRecords = new QuestionsRecords();
        questionsRecords.setQuestionsId(questionId);
        String s = "";
        if (operationId == 1) s = "上架";
        if (operationId == 2) s = "下架";
        questionsRecords.setOperation(s);
        questionsRecords.setQuestionsId(questionId);
        questionsRecords.setSetTime(new Date());
        HttpSession session = request.getSession();
        Integer setterID = (Integer) session.getAttribute("setterID");
        questionsRecords.setId(setterID);
        questionsRecordsMapper.insert(questionsRecords);
    }

    @Override
    public PageResult findAllBest(Integer page, Integer pagesize) {
        IPage<Questions> p = new Page<>(page, pagesize);
        LambdaQueryWrapper<Questions> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Questions::getIsChoice, 1);
        IPage<Questions> questionsIPage = questionsMapper.selectPage(p, wrapper);
        return new PageResult(questionsIPage.getTotal(), questionsIPage.getSize(), questionsIPage.getPages(), questionsIPage.getCurrent(), questionsIPage.getRecords());
    }
   //组题列表
    @Override
    public PageResult pageList(Query query) {
        IPage<Questions> p=new Page<>(query.getPage(),query.getPagesize());
        String keyword = query.getKeyword();
        LambdaQueryWrapper<Questions> wrapper=new LambdaQueryWrapper<>();
        if(keyword!=null|keyword!=""){
            wrapper.eq(Questions::getNumber,keyword);
        }
        IPage<Questions> pl = questionsMapper.selectPage(p, wrapper);
        return new PageResult(pl.getTotal(),pl.getSize(),pl.getPages(),pl.getCurrent(),pl.getRecords());
    }
    //6.12移除或加入精选
    @Override
    public void removeBest(Integer id, Integer choiceState) {
        Questions questions = questionsMapper.selectById(id);
        questions.setIsChoice(choiceState);
        LambdaQueryWrapper<Questions> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Questions::getId, id);
        questionsMapper.update(questions, wrapper);
    }

    //6.13试题审核
    @Override
    public R auditById(Integer id, Body body) {
        Questions questions = questionsMapper.selectById(id);
        questions.setChkState(body.getChkState());
        questions.setChkRemarks(body.getChkRemarks());
        LambdaQueryWrapper<Questions> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Questions::getId, id);
        questionsMapper.update(questions, wrapper);
        return new R(true);
    }

}
